﻿#include "DeviceInfoManager.h"

#include "Device.h"
#include "UserInfoCodec.h"

DeviceInfoManager::DeviceInfoManager()
{
  connect(&timer_updateDeviceInfoList,&QTimer::timeout,[this](){userInfoCodec->getAllDeviceFromServer();});
}

DeviceInfoManager::~DeviceInfoManager()
{

}

bool DeviceInfoManager::contains(uint deviceId)
{
  return deviceInfoListIndex.contains(deviceId);
}

Device* DeviceInfoManager::getDeviceInfoByDeviceId(uint deviceId)
{
  if(!deviceInfoListIndex.contains(deviceId))
    {
      return Q_NULLPTR;
    }
  return &deviceInfoList[deviceInfoListIndex.value(deviceId)];
}

void DeviceInfoManager::startTimer_allDevice(int msec)
{
  timer_updateDeviceInfoList.start(msec);
}

void DeviceInfoManager::stopTimer_allDevice()
{
  timer_updateDeviceInfoList.stop();
}

QVector<Device>* DeviceInfoManager::getDeviceInfoList()
{
  return &deviceInfoList;
}

DeviceInfoManager* DeviceInfoManager::setDeviceInfoList(QVector<Device>* const deviceInfoList)
{
  this->deviceInfoList=*deviceInfoList;
  deviceInfoListIndex.clear();
  uint i=0;
  for(Device& deviceInfo:*deviceInfoList)
    {
      deviceInfoListIndex.insert(deviceInfo.getDeviceId(),i++);
    }
  emit signal_newAllDeviceInfo();
  return this;
}

UserInfoCodec* DeviceInfoManager::getUserInfoCodec()const
{
  return userInfoCodec;
}

DeviceInfoManager* DeviceInfoManager::setUserInfoCodec(UserInfoCodec*const userInfoCodec)
{
  this->userInfoCodec=userInfoCodec;
  printInjection(this,userInfoCodec);
  return this;
}

void DeviceInfoManager::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setUserInfoCodec(getObjFromMap<UserInfoCodec>(objMap));
}
